package com.qf.service.impl;

import com.qf.bean.Teacher;
import com.qf.dao.TeacherDao;
import com.qf.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 2019/11/30
 * Administrator
 * springdata1130
 * 面向对象面向君  不负代码不负卿
 */
@Service
public class TeacherServiceImpl  implements TeacherService {

    @Transactional
    public Teacher insertTea(Teacher teacher) {
        Teacher save = teacherDao.save(teacher);
        System.out.println(5/0);
        return save;
    }

    public List<Teacher> findbytnameAndsex(String name, String sex) {
        return teacherDao.findByTnameAndTsex(name,sex);
    }

    public List<Teacher> findbyname(String tname) {
        return teacherDao.findByTnameLike(tname);
    }

    public List<Teacher> findall() {
        Iterable<Teacher> all = teacherDao.findAll();
        Iterator<Teacher> iterator = all.iterator();
        ArrayList arrayList = new ArrayList();
        while(iterator.hasNext()){
            Teacher next = iterator.next();
            arrayList.add(next);
        }
        return arrayList;
    }

    public int countByTnameContaining(String name) {
        return teacherDao.countByTnameContaining(name);
    }

    public Page<Teacher> findByTidIn(List list, Pageable pageable) {
        return teacherDao.findByTidIn(list,pageable);
    }

    public List<Teacher> findAll(Sort sort) {
        return teacherDao.findAll(sort);
    }

    public List<Teacher> findByTidIn(List<Integer> ids) {
        return teacherDao.findByTidIn(ids);
    }

    public List<Teacher> findByTnameStartingWith(String name) {
        return teacherDao.findByTnameStartingWith(name);
    }

    public List<Teacher> findgreater(int tid) {
        return teacherDao.findByTidGreaterThan(tid);
    }

    public Teacher updateTeacher(Teacher teacher) {
        Teacher save = teacherDao.save(teacher);
        return save;
    }

    @Autowired
    private TeacherDao teacherDao;

    public List<Teacher> findbyids(int begin, int end) {
        return teacherDao.findByTidBetween(begin,end);
    }

    public Teacher findbyid(int tid) {
        return teacherDao.findOne(tid);
    }
}
